package com.example.broadcastnotification;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button mButton;
    private Button mButton1;
    private Button mButton2;
private MyReceiver mMyReceiver = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        mButton = findViewById(R.id.bt1);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyReceiver.ACTION);
                sendBroadcast(intent);
            }
        });
        mButton1 = findViewById(R.id.bt2);
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == mMyReceiver){
                    mMyReceiver= new MyReceiver();
                registerReceiver(mMyReceiver,new IntentFilter(MyReceiver.ACTION));
                }
            }
        });
        mButton2 = findViewById(R.id.bt3);
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMyReceiver!=null){
                    unregisterReceiver(mMyReceiver);
                    mMyReceiver = null;
                }
            }
        });
    }
}
